package com.qdaily.ui.ui.views;

import android.content.Context;
import android.graphics.Canvas;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.qdaily.supportlibrary.util.LocalDisplay;
import com.qdaily.supportlibrary.util.QLog;
import com.qdaily.ui.R;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

/**
 * Created by song on 15/5/15.
 */
public class QDLoadMoreView extends FrameLayout {
    private String TAG = "QDLoadMoreView";
    private GifImageView gifview;
    private GifDrawable gifDrawable;
    public QDLoadMoreView(Context context)
    {
        super(context);
        initView();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

    }

    private void initView()
    {

    }

    @Override
    public void setVisibility(int visibility) {
        if (gifview != null)
        {
            gifview.setVisibility(visibility);
        }
        super.setVisibility(visibility);
    }

    public void stop()
    {
        if (gifDrawable != null)
        {
            gifview.setImageBitmap(null);
            gifview.setImageDrawable(null);
            gifDrawable.recycle();
            gifview = null;
            gifDrawable = null;
        }
    }

    public void pause()
    {
        if (gifDrawable != null)
        {
            gifDrawable.pause();
        }
    }

    public void start(){
        if (gifDrawable != null)
        {
            gifDrawable.start();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        QLog.w(TAG, "onDetachedFromWindow");
        stop();
        super.onDetachedFromWindow();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        QLog.w(TAG, "onAttachedToWindow");
        if (gifview == null)
        {
            gifview = new GifImageView(getContext());
            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LocalDisplay.dp2px(100));
            gifview.setLayoutParams(lp);
            gifview.setImageResource(R.drawable.loadmore);
            gifview.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            addView(gifview);
            gifDrawable = (GifDrawable)gifview.getDrawable();
        }
    }
}
